<?php
include 'theme_options.php';
include 'guide.php';
if ( function_exists('register_sidebar') )
    register_sidebar(array(
	'name' => 'Sidebar',
    'before_widget' => '<div class="sidebox rounded">',
    'after_widget' => '</div>',
	'before_title' => '<h3 class="sidetitl">',
    'after_title' => '</h3>',
    ));
	


register_nav_menus( array(
		'primary' => __( 'Primary Navigation', '' ),

	) );



function post_type_videos() {
register_post_type(
                     'videos', 
                     array( 'public' => true,
					 		'publicly_queryable' => true,
							'hierarchical' => false,
							'menu_icon' => get_stylesheet_directory_uri() . '/images/video.png',
                    		'labels'=>array(
    									'name' => _x('Videos', 'post type general name'),
    									'singular_name' => _x('Video', 'post type singular name'),
    									'add_new' => _x('Add New', 'video'),
    									'add_new_item' => __('Add New video'),
    									'edit_item' => __('Edit video'),
    									'new_item' => __('New video'),
    									'view_item' => __('View video'),
    									'search_items' => __('Search video'),
    									'not_found' =>  __('No videos found'),
    									'not_found_in_trash' => __('No videos found in Trash'), 
    									'parent_item_colon' => ''
  										),							 
                             'show_ui' => true,
							 'menu_position'=>5,
                             'supports' => array(
							 			'title',
										'editor',
										'author',
                                        'post-thumbnails',
                                        'excerpts',
                                        'trackbacks',
							            'custom-fields',
                                        'comments',
                                        'revisions')
							

                                ) 
                      );
				  

} 
add_action('init', 'post_type_videos');

function create_genre_taxonomy() 
{
  $labels = array(
	  						  'name' => _x( 'Genre', 'taxonomy general name' ),
    						  'singular_name' => _x( 'genre', 'taxonomy singular name' ),
    						  'search_items' =>  __( 'Search genre' ),
   							  'all_items' => __( 'All Genre' ),
    						  'parent_item' => __( 'Parent Genre' ),
   					   		  'parent_item_colon' => __( 'Parent Genre:' ),
   							  'edit_item' => __( 'Edit Genre' ), 
  							  'update_item' => __( 'Update Genre' ),
  							  'add_new_item' => __( 'Add New Genre' ),
  							  'new_item_name' => __( 'New Genre Name' ),
  ); 	

  register_taxonomy('genre',array('videos'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'genre' ),
  ));

}
add_action( 'init', 'create_genre_taxonomy', 0 );





$new_meta_boxes =
array(
	"trailer" => array(
	"name" => "trailer",
	"std" => "",
	"title" => "Trailer video",
	"description" => "
		<p>Enter the video url</p>
	
	"
),	"poster" => array(
	"name" => "poster",
	"std" => "",
	"title" => "Movie poster",
	"description" => "
		<p> Movie poster image</p>
	
	"
),

);
function new_meta_boxes() {
global $post, $new_meta_boxes;

foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);

if($meta_box_value == "")
$meta_box_value = $meta_box['std'];

echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';

echo'<h2 style=" margin:0px; padding:0px 3px; font-style:normal; font-family:Tahoma; font-size:13px;font-weight:bold;">'.$meta_box['title'].'</h2>';

echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="95" /><br />';

echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
}
}
function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Trailer data', 'new_meta_boxes', 'videos', 'normal', 'high' );
}
}
function save_postdata( $post_id ) {
global $post, $new_meta_boxes;

foreach($new_meta_boxes as $meta_box) {
// Verify
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}

$data = $_POST[$meta_box['name'].'_value'];

if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
update_post_meta($post_id, $meta_box['name'].'_value', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
